﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.Common;

namespace Waker.Devices
{
    public partial class FormNVRSetting : Base.FormBaseToolBar
    {
        public FormNVRSetting()
        {
            InitializeComponent();
        }

        private void FormNVRSetting_Load(object sender, EventArgs e)
        {
            FormRefresh();
        }
        public override void FormRefresh()
        {
            var dt = new BAL.BAL_Device().GetTableByTableName("T_NVR");
            this.gridControl1.DataSource = dt;
        }
        protected override void FormAdd()
        {
            var f = new FormNVREdit(0);
            if (f.ShowDialog() == DialogResult.OK)
                FormRefresh();
        }
        protected override void FormEdit()
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var dr = gridView1.GetFocusedDataRow();
            if (dr == null)
                return;
            var f = new FormNVREdit(Convert.ToInt32(dr["NVR_ID"]));
            if (f.ShowDialog() == DialogResult.OK)
            {
                var formMain = this.MdiParent as FormMain;
                if (formMain != null)
                    formMain.InitADS();
                FormRefresh();
            }
        }
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks > 1)
            {
                FormEdit();
            }
        }
        protected override void FormDelete()
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var dr = gridView1.GetFocusedDataRow();
            if (dr == null)
                return;
            if (Session._DicADsDevice == null)
                return;
            var id = Convert.ToInt32(dr["NVR_ID"]);
            //bool isHash = false;
            //var listSubs = Session._DicADsDevice.Select(drive => drive.Value.ListSub);
            //if (listSubs != null && listSubs.Count() > 0)
            //{
            //    foreach (var list in listSubs)
            //    {
            //        if (list.Where(sub => sub.IsUseNVR && sub.NVR_ID == id).FirstOrDefault() != null)
            //        {
            //            isHash = true;
            //            break;
            //        }
            //    }
            //}
            //if (isHash)
            //{
            //    UI.ShowError("删除失败,失败原因:有门禁设备正在使用此设备抓图.\r\n请在门禁设备中移除后重试");
            //    return;
            //}
            var isOK = new BAL.BAL_Device().DeleteNVR(id);
            if (isOK)
            {
                UI.ShowInfo("删除成功");
                var formMain = this.MdiParent as FormMain;
                if (formMain != null)
                    formMain.InitADS();
                FormRefresh();

            }
            else
            {
                UI.ShowError("删除失败");
            }
        }
    }
}
